define(['app','pagination','datetime','js/directive/listTree','js/filter/tree2Arr','exporting','autocomplete', 'editor'], function(app) {
    'use strict';
   app.controller('Dimission', function($scope, $http, dataFactoryCRM , $q, $timeout) {
            $scope.search_param = {number:'',nickname:'',sector:-1,role:-1,leaveType:-2,dimissiontime1:'',dimissiontime2:'',state:''};
            $scope.editData = {user_id:0};
            $scope.addData = {dimissionTime:'',state:'',reson:'',beizhu:''}
            $scope.teacherList = [{}];
            $scope.userRoleSector = {user_id:0};              
           /*分页*/
            $scope.pagerdata = {pageAll: 1,pageNow: 1, pageSize:$scope.psize,k: 3};
            $scope.user;
            $scope.leaveType = [{ k: '2', v: '辞职' }, { k: '-1', v: '解聘' },{ k: '1', v: '开除' }, { k: '0', v: '其他' }];
            /*获取分页数据*/
            $scope.getPageData = function(){
                var sUrl = "dimission/index";
                var _param =  $scope.search_param;
                _param['psize'] = $scope.pagerdata.pageSize;
                _param['p'] = $scope.pagerdata.pageNow;
                sUrl += "?"+$.param( _param ) ;
                dataFactoryCRM.getDataByAPI( sUrl ).then( function( _d ){
                    console.log(_d);
                    $scope.userseee = _d.data;
                    $scope.rcode = _d.rcode;
                    $scope.pagerdata.pageSize = _d.psize;
                    $scope.pagerdata.pageAll = _d.pall;
                    $scope.pagerdata.pageNow = _d.p;
                    $scope.$emit("NOTBUSY");
                });
            }
            $scope.getPageData();
            $scope.search = function(){
                $scope.getPageData();
            }
           /* 加载部门*/
            $scope.busy = dataFactoryCRM.getDataByAPI('user/getPageParam')
                    .then( function( data ) {
                    $scope.busy = false;
                    if( data ) {
                        $scope.sectors = data.sector;
                        $scope.roles = data.role;
                        // console.log( $scope.sectors);
                    }
                });
            /*成功弹窗弹出事件*/
            $scope.successFun = function(){
                    $('.ui.modal.OK').modal('show');
                    setTimeout(function() {
                        $('.ui.modal.OK').modal('hide')
                    }, 3000)
                }
            /*失败弹窗弹出事件*/
                $scope.failFun = function(){
                    $(".ui.modal.Fail").modal('show');
                    setTimeout(function() {
                        $('.ui.modal.Fail').modal('hide')
                    }, 3000);
                }
            /*用户搜索*/
            $scope.search = function(){
                $scope.getPageData();
            }
            /*员工自动补全*/
            $scope.teacherInputChanged = function($item) {
                $scope.detectTeacher = $item;
            }

            /*添加-监听输入员工信息*/
            $scope.$watch('detectTeacher', function(newvalue, oldvalue) {
                if (newvalue != oldvalue) {
                    dataFactoryCRM.postDataByAPI('dimission/addDimissionShow', 'nickname=' + $scope.detectTeacher)
                        .then(function(d) {
                            console.log(d);
                            if (d.rcode) {
                                $scope.teacherList = d.data;
                            } else {
                                $scope.teacherList = [{}];
                            }
                        });
                }
            });
            /*撤销离职*/
            $scope.deleteDimission = function(userid){
                   var _vuser = this.vuser;
                 dataFactoryCRM.postDataByAPI('dimission/deleteDimission',_vuser)
                        .then(function(d){
                            console.log(d);
                            $scope.userMsg = d.msg;
                             if(d.rcode){
                                 $scope.msg = '撤销离职成功';
                                $scope.successFun();
                                 $scope.getPageData();
                                return true;
                             }else{
                                 $scope.msg = '撤销离职失败';
                                 $scope.failFun();
                                 return false;
                            }
                            });
            }
            /*新增离职*/
            $scope.addDimission = function(){
                $("#addfrm").form('clear');
                $scope.$broadcast('angucomplete-alt:clearInput');
                function _addUser( user ){
                 $scope.busy = dataFactoryCRM.postDataByAPI('dimission/addDimission',user )
                    .then(function(d){
                        console.log(d);
                        if(d.rcode){
                            $scope.msg = '新增离职成功';
                            $scope.successFun();
                            $scope.getPageData();
                            return true;
                          }else{
                            $scope.msg = '新增离职失败';
                            $scope.failFun();
                            return false;
                        }
                 
                    });
                }
                $('.ui.addDimission').modal({
                    onApprove: function() {
                        if( $('#addfrm').form('is valid') ){
                             _addUser($('#addfrm').serialize());
                        }
                        else
                            return false;
                    }}).modal('show');
            }
            /*查看离职*/
            $scope.lookDimission = function( dim_id) {
                var _vuser = this.vuser;
                $scope.lookData = _vuser;
                console.log($scope.lookData);
                 $('.ui.look.modal').modal('show');                   
            },
            /*编辑*/
            $scope.editDimission = function() {
                var _vuser = this.vuser;
                $scope.nickname = _vuser.ext_name;
                $scope.dim_id = _vuser.dim_id;
                $scope.ext_leavetime = _vuser.dim_date;
                $scope.ext_leave_reason = _vuser.dim_reason;
                $('#edit_leaveType').dropdown('set selected', _vuser.dim_type);
                $scope.ext_leave_beizhu =_vuser.dim_beizhu;
                function _updateDimission( user ) {
                     console.log(user);
                    dataFactoryCRM.postDataByAPI('dimission/saveEdit', user )
                        .then(function(d){
                            console.log(d);
                            $scope.userMsg = d.msg;
                            if(d.rcode){
                                $scope.msg = '编辑离职成功';
                                $scope.successFun();
                                $scope.getPageData();
                                return true;
                                    }else{
                                        $scope.msg = '编辑离职失败';
                                        $scope.failFun();
                                        return false;
                                    }
                            });
                  }
                   $("#dim_id").val(_vuser.dim_id);
                $("#update_teacher").val(_vuser.user_id);
                $scope.detectTeacher = _vuser.nickname;
                $("#update_user_id").val(_vuser.user_id);
                $("#update_teachername_value").val(_vuser.nickname);
                $('.ui.edit.modal').modal({
                    autofocus:false,
                    onApprove:function(){
                        if( $('#editfrm').form('is valid') ){
                          _updateDimission($('#editfrm').serialize());
                         }else{
                         $scope.api_error(' 数据更新失败');
                        }
                    } 
                }) .modal('show');                   
            },
         $scope.cancel_show     = function(){
            $(".ui.remind").modal('show');
           
        }
             /**添加-投资顾问自动补全**/
            $scope.selecteTeacher = function(selected){
               if (selected){
                    $("#add_teacher").val(selected.description.user_id);
                }else{
                    $("#add_teacher").val('');
                }
            }

            setTimeout(function() {
                /*add*/
                registerHiddenBlur('add_teacher', 'add_managername_value');
                /*update*/
                registerHiddenBlur('update_teacher', 'update_teachername_value');
            }, 500);
    });
})